package http;

import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Response;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

/**
 * Created by root on 15-5-28.
 */
public class SimpleHttpResponse {

    private int statusCode;
    private String reason;
    private String text;
    private Header[] headers;

    public SimpleHttpResponse(int statusCode, String reason, String text, Header[] headers) {
        this.statusCode = statusCode;
        this.reason = reason;
        this.text = text;
        this.headers = headers;
    }

    public int getStatusCode() {
        return statusCode;
    }

    public String getReason() {
        return reason;
    }

    public String getText() {
        return text;
    }

    public Header[] getHeaders() {
        return headers;
    }

    public static SimpleHttpResponse from (HttpResponse r) {
        int status_code = r.getStatusLine().getStatusCode();
        String reason = r.getStatusLine().getReasonPhrase();
        String body = null;
        if (r != null && r.getEntity() != null) {
            try {
                body = IOUtils.toString(r.getEntity().getContent(), Charsets.UTF_8);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return new SimpleHttpResponse(status_code, reason, body, r.getAllHeaders());
    }

    public static SimpleHttpResponse from (Response r) throws IOException {
//        int statusCode = r.returnResponse().getStatusLine().getStatusCode();
//        String reason = r.returnResponse().getStatusLine().getReasonPhrase();
//        InputStreamReader ir = new InputStreamReader(r.returnResponse().getEntity().getContent());
//        char [] content = {};
//        ir.read(content);
//        String text = String.valueOf(content);
//        return new SimpleHttpResponse(statusCode, reason, text);
        return from(r.returnResponse());
    }

    @Override
    public String toString () {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("%s %s\n-\n", statusCode, reason));
        for (Header header : this.headers) {
            sb.append(String.format("%s: %s\n", header.getName(), header.getValue()));
        }
        sb.append("-\n");
        sb.append(this.text);
        return sb.toString();
    }

    public String toString (int indent) {
        try {
            StringBuilder sb = new StringBuilder();
            sb.append(String.format("%s %s\n-\n", statusCode, reason));
            for (Header header : this.headers) {
                sb.append(String.format("%s: %s\n", header.getName(), header.getValue()));
            }
            sb.append("-\n");
            sb.append(new JSONObject(this.text).toString(indent));
            return sb.toString();
        } catch (JSONException e) {
            // response body is not in json format, return in normal format
            return toString();
        }
    }

    public void show () {
        System.out.println(this.toString());
    }
    public void show (int indent) {
        System.out.print(this.toString(indent));
    }
//    public void show (Logger logger) {
//        logger.info(this.toString());
//    }
//    public void show (Logger logger, int indent) {
//        logger.info(this.toString(indent));
//    }

}
